/**
*
*黄国亨 2018-07-03
* js文件下载工具类
*/


(function (win) {

    var frameIdName = "DOWNLOAD_FILE_",
        id = 1;

    function DownLoad(options) {

        this.options = options;
       
    }

    DownLoad.prototype = {

        clone: function (data) {
            var d = {};
            if (Object.prototype.toString.call(data) == "[object Array]" || Object.prototype.toString.call(data) == "[object Object]") {

            }
            return JSON.parse(JSON.stringify(data));
            
        },
        init2: function (options) {
            this.options = this.clone(options);
        },
        init: function () {
            this.$element = win.document.createElement('div');
            this.$element.style.display = 'none';
            
           
            this.iframe=frameIdName + (id++);

            var form = this.createForm(this.options.data, this.options.url, this.options.method, this.iframe);


         

            var self=this;

            //iframe.onload = iframe.onreadystatechange = function () {
            //    console.log("wancheng");
            //    if (this.readyState && this.readyState != 'complete') return;
            //    else {
            //        if (self.options.finish)
            //            self.options.finish();
            //    }
            //}
          

            //iframe.onerror = iframe.onreadystatechange = function () {
            //    console.log("wancheng");
            //    if (this.readyState && this.readyState != 'complete') return;
            //    else {
            //        self.options.finish(false);
            //    }
            //}

           
            //延迟提交表单
            //var timeout = win.setTimeout(function () {
                var iframe = self.createIframe(self.iframe);
              
                
                if (iframe.attachEvent) {
                    iframe.attachEvent("onreadystatechange", function () {
                        //此事件在内容没有被载入时候也会被触发，所以我们要判断状态
                        //有时候会比较怪异 readyState状态会跳过 complete 所以我们loaded状态也要判断
                        if (iframe.readyState === "complete" || iframe.readyState == "loaded") {
                            //代码能执行到这里说明已经载入成功完毕了
                            //要清除掉事件
                            //iframe.detachEvent("onreadystatechange", arguments.callee);
                            //这里是回调函数
                            if (self.options.finish) {
                                self.options.finish();
                                //self.destroy();
                            }
                        }
                    });
                } else {
                    iframe.addEventListener("load", function () {
                        //代码能执行到这里说明已经载入成功完毕了
                        //this.removeEventListener("load", arguments.call, false);
                        //这里是回调函数
                        if (self.options.finish) {
                            self.options.finish();
                            //self.destroy();
                        }
                    }, false);
                }

                win.document.getElementsByTagName('BODY')[0].appendChild(self.$element);


                //表单提交
                form.submit();


              
            //   win.clearTimeout(timeout);
            //},0);
        },

        //创建并且返回表单
        createForm:function(data,url,method,iframe) {
            var inputStr = '';

            method = method || 'post';
            ///生产input的值
            for (var i in data) {

                if (Object.prototype.toString.call(data[i]) == "[object Object]" || Object.prototype.toString.call(data[i]) == "[object Array]") {
                    inputStr += '<input name="' + i + '" value="' + JSON.stringify( data[i]) + '" />'
                } else {
                    inputStr += '<input name="' + i + '" value="' + data[i] + '" />'
                }
                
            }

            var form = '<form action="' + url + '" method="' + method + '" enctype="application/x-www-form-urlencoded" target="'+iframe+'">' + inputStr + '</form>'

            
            this.$element.innerHTML = form;

            //返回创建的表单
            return this.$element.querySelector('form');
            
        },

        createIframe:function(iframeName){
            var el = win.document.createElement('div');

            el.innerHTML = "<iframe name='" + iframeName + "'></iframe>";
            this.$element.appendChild(el);
            
            var iframe = el.querySelector('iframe');



            return iframe;
        },

        /**
        *销毁form
        */
        destroy: function () {
            if (this.$element)
                this.$element.parentNode.removeChild(this.$element);
            this.$element = null;
        },

        //reInit: function () {
        //    this.init();
        //},
        download: function (data) {
            if (data) {
                this.options = data;
            }

            this.init();
        }
    }

   

    win.download = DownLoad;

})(window);